package com.chy.blog_server.modular.web.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.blog_server.core.util.UploadFileUtil;
import com.chy.blog_server.core.util.UploadURLUtil;
import com.chy.blog_server.modular.web.entity.BoPhotoAlbum;
import com.chy.blog_server.modular.web.mapper.BoPhotoAlbumMapper;
import com.chy.blog_server.modular.web.service.IBoPhotoAlbumService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：相册表 service业务层
 * 作者：chenhuiyan
 * 时间：2020/02/25 12:03
 */
@Service
@Transactional(readOnly = true)
public class BoPhotoAlbumServiceImpl extends ServiceImpl<BoPhotoAlbumMapper, BoPhotoAlbum> implements IBoPhotoAlbumService {

    private final static Logger logger = LoggerFactory.getLogger(BoPhotoAlbumServiceImpl.class);

    /**
     * 查询列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> list(Map<String, Object> params){
        return baseMapper.list(params);
    }

    /**
     * 查询总数
     * @param params
     * @return
     */
    public int getCount(Map<String, Object> params){
        return baseMapper.getCount(params);
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    public Map<String, Object> getInfoById(String id){
        return baseMapper.getInfoById(id);
    }

    /**
     * 保存（并删除封面图）
     * @param boPhotoAlbum
     * @param request
     * @return
     */
    @Transactional(readOnly = false)
    public Map<String, Object> saveData(BoPhotoAlbum boPhotoAlbum, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        if(StrUtil.isEmpty(boPhotoAlbum.getId())){
            boPhotoAlbum.preInsert(request);
            this.save(boPhotoAlbum);
            res.put("state", "200");
            res.put("message", "添加成功");
        }else{

            //比较封面图是否重新上传，是则删除原封面图(不是默认的封面图)
            BoPhotoAlbum boPhoto = this.getById(boPhotoAlbum.getId());
            if(!boPhotoAlbum.getImg().equals(boPhoto.getImg()) && !UploadURLUtil.PHOTO_ALBUM_IMG_NAME.equals(boPhoto.getImg())){
                boolean flag = UploadFileUtil.deleteOneFile(UploadURLUtil.PHOTO_ALBUM_IMG + boPhoto.getImg());
                if(flag){
                    logger.info(">>>>>>>>>>>>删除封面图成功(" + boPhoto.getImg() + ")");
                }else{
                    logger.info(">>>>>>>>>>>>删除封面图失败(" + boPhoto.getImg() + ")");
                }
            }

            boPhotoAlbum.preUpdate(request);
            this.updateById(boPhotoAlbum);
            res.put("state", "200");
            res.put("message", "修改成功");
        }
        return res;
    }

    /**
     * 查询已删除的相册列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> recycleList(Map<String, Object> params){
        return baseMapper.recycleList(params);
    }

    /**
     * 彻底删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int thoroughlyDelete(String id){
        //比较封面图是否重新上传，是则删除原封面图(不是默认的封面图)
        BoPhotoAlbum boPhoto = this.getById(id);
        if(!UploadURLUtil.PHOTO_ALBUM_IMG_NAME.equals(boPhoto.getImg())){
            boolean flag = UploadFileUtil.deleteOneFile(UploadURLUtil.PHOTO_ALBUM_IMG + boPhoto.getImg());
            if(flag){
                logger.info(">>>>>>>>>>>>删除封面图成功(" + boPhoto.getImg() + ")");
            }else{
                logger.info(">>>>>>>>>>>>删除封面图失败(" + boPhoto.getImg() + ")");
            }
        }
        return baseMapper.thoroughlyDelete(id);
    }

    /**
     * 查询阅读数量总数
     * @return
     */
    public int getReadNumTotal(){
        return baseMapper.getReadNumTotal();
    }

    /**
     * 查询赞数量总数
     * @return
     */
    public int getPraiseNumTotal(){
        return baseMapper.getPraiseNumTotal();
    }

}
